package com.yangwp.jie.controller;

import com.yangwp.jie.common.enums.RetCodeEnum;
import com.yangwp.jie.common.utils.MapUtils;
import com.yangwp.jie.model.JieArticle;
import com.yangwp.jie.service.JieArticleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/jie")
public class EditController {

    @Autowired
    private JieArticleService jieArticleService;

    @RequestMapping("/edit")
    public String edit(){
        return "/main/edit.ftl";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(@RequestBody JieArticle jieArticle){

        if(jieArticle == null || StringUtils.isBlank(jieArticle.getContentText()) || StringUtils.isBlank(jieArticle.getContentTitle())){
            return MapUtils.getRetMap(RetCodeEnum.MISSING_PARAM);
        }

        if(jieArticleService.save(jieArticle)){
            return MapUtils.getRetMap(RetCodeEnum.SUCCESS);
        }
        return MapUtils.getRetMap(RetCodeEnum.SERVER_FAIL);
    }

}
